{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 三大结构\n",
    "- 顺序呢\n",
    "- 分支\n",
    "- 循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 分支\n",
    "- 语法\n",
    "            if 条件表达式:\n",
    "                语句1\n",
    "                语句2\n",
    "                ...\n",
    "- 条件表达式就是计算结果必须为布尔值的表达式\n",
    "- 注意表达式后面的冒号不能少\n",
    "- 注意if 后面出现的语句，如果属于if 语句块，则必须同一个缩进等级\n",
    "- 条件表达式为True 时执行if后面的语句块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你未成年\n",
      "回家玩吧\n",
      "结束\n"
     ]
    }
   ],
   "source": [
    "# if\n",
    "a = 17\n",
    "if a<18:\n",
    "    print('你未成年')\n",
    "    print('回家玩吧')\n",
    "    print('结束')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "老司机，开车了\n"
     ]
    }
   ],
   "source": [
    "a = 19\n",
    "if a<18:\n",
    "    print('你未成年')\n",
    "    print('回家玩吧')\n",
    "    print('结束')\n",
    "print('老司机，开车了')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "今天学习for\n",
      "开始学习\n"
     ]
    }
   ],
   "source": [
    "print('今天学习for')\n",
    "gender = '男'\n",
    "if gender == '女':\n",
    "    print('发糖吃 ')\n",
    "    \n",
    "print('开始学习')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 双向分支\n",
    "- if ... else ...\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入性别：nn\n",
      "你输入的性别是:nn\n",
      "输入的不对啊\n",
      "结束了\n"
     ]
    }
   ],
   "source": [
    "gender = input(\"请输入性别：\")\n",
    "print(\"你输入的性别是:{0}\".format(gender))\n",
    "\n",
    "if gender == \"n\":\n",
    "    print('OK，你通过了')\n",
    "else:\n",
    "    print('输入的不对啊')\n",
    "\n",
    "print('结束了')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 多路分支\n",
    "- 很多分支的情况，简称多路分支\n",
    "\n",
    "        if 条件表达式：\n",
    "            语句1\n",
    "            ...\n",
    "        elif 条件表达式2：\n",
    "            语句2\n",
    "        ...\n",
    "        else:\n",
    "            语句else\n",
    "            \n",
    "- elif 可以有很多\n",
    "- else 是可选的\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入成绩：90\n",
      "B\n"
     ]
    }
   ],
   "source": [
    "score = input('输入成绩：')\n",
    "score = int(score)\n",
    "\n",
    "if score>90:\n",
    "    print('A')\n",
    "elif score>80:\n",
    "    print('B')\n",
    "elif score>70:\n",
    "    print('C')\n",
    "elif score>60:\n",
    "    print('D')\n",
    "else:\n",
    "    print('Fuck')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## if语句其他：\n",
    "- if语句可以嵌套使用，但不推荐\n",
    "- Python没有switch-case 语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 循环语句\n",
    "- for循环 \n",
    "- while循环\n",
    "## for循环\n",
    "- for\n",
    "        for 变量 in 序列：\n",
    "            语句1\n",
    "            语句2\n",
    "            ...\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 列表:一列数字或其他值,一般用中括号表示\n",
    "# eg: ['zhangsan','lisi','wangwu']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhangsan\n",
      "lisi\n",
      "wangwu\n",
      "zhaoliu\n"
     ]
    }
   ],
   "source": [
    "students = ['zhangsan','lisi','wangwu','zhaoliu']\n",
    "for stu in students:\n",
    "    print(stu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhangsan\n",
      "NO\n",
      "lisi\n",
      "wwwww:lisi\n",
      "wangwu\n",
      "NO\n",
      "zhaoliu\n",
      "NO\n"
     ]
    }
   ],
   "source": [
    "students = ['zhangsan','lisi','wangwu','zhaoliu']\n",
    "for stu in students:\n",
    "    print(stu)\n",
    "    if stu =='lisi':\n",
    "        print('wwwww:{0}'.format(stu))\n",
    "    else:\n",
    "        print('NO')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## range 介绍\n",
    "- 生成一系列数字\n",
    "- 可以设定具体范围"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "----------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([9])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# range 练习;\n",
    "# range(1,10), 前包含，后不包含\n",
    "# randint 是特例，前后都包含\n",
    "import numpy as np\n",
    "\n",
    "for i in range(1,10):\n",
    "    print(i)\n",
    "print('-'*10)\n",
    "\n",
    "np.random.randint(3,10,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for-else 语句\n",
    "- 当for循环结束的时候，会执行else语句\n",
    "- else 语句是可选语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhangsan\n",
      "NO\n",
      "lisi\n",
      "wwwww:lisi\n",
      "wangwu\n",
      "NO\n",
      "zhaoliu\n",
      "NO\n",
      "---over---\n"
     ]
    }
   ],
   "source": [
    "students = ['zhangsan','lisi','wangwu','zhaoliu']\n",
    "for stu in students:\n",
    "    print(stu)\n",
    "    if stu =='lisi':\n",
    "        print('wwwww:{0}'.format(stu))\n",
    "    else:\n",
    "        print('NO')\n",
    "else:\n",
    "    print('---over---')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 循环之 break;continue;pass\n",
    "- break:无条件结束整个循环，简称循环猝死\n",
    "- continue：无条件结束本次循环，从新进入下一轮循环\n",
    "- pass： 表示路过"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xxxxx\n",
      "xxxxx\n",
      "xxxxx\n",
      "xxxxx\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "get it\n",
      "偶数2\n",
      "偶数4\n",
      "偶数6\n",
      "偶数8\n",
      "偶数10\n",
      "偶数2\n",
      "偶数4\n",
      "偶数6\n",
      "偶数8\n",
      "偶数10\n",
      "2是偶数\n",
      "4是偶数\n",
      "6是偶数\n",
      "8是偶数\n",
      "10是偶数\n",
      "xxx\n",
      "xxx\n",
      "xxx\n",
      "xxx\n",
      "xxx\n",
      "xxx\n",
      "xxx\n",
      "xxx\n",
      "xxx\n",
      "xxx\n"
     ]
    }
   ],
   "source": [
    "for _ in range(1,5):\n",
    "    print('xxxxx')\n",
    "# break\n",
    "for i in range(1,11):\n",
    "    if i==7:\n",
    "        print('get it')\n",
    "        break;\n",
    "    else:\n",
    "        print(i)\n",
    "# continue\n",
    "for i in range(1,11):\n",
    "    if i%2==0:\n",
    "        print('偶数{}'.format(i))\n",
    "    else:\n",
    "        continue\n",
    "\n",
    "for i in range(1,11):\n",
    "    if i%2==0:\n",
    "        print('偶数{}'.format(i))\n",
    "\n",
    "for i in range(1,11):\n",
    "    if i%2==1:\n",
    "        continue\n",
    "    print('{}是偶数'.format(i))\n",
    "\n",
    "# pass 一般用于占位\n",
    "for i in range(1,11):\n",
    "    pass\n",
    "    print('xxx')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
